package com.wzdigit.wms.wms.service.core.task;

import cn.hutool.core.collection.CollectionUtil;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.mes.qms.dto.WipRcardCompletionDataDto;
import com.wzdigit.wms.basic.mapper.core.wip.WipGoodsMapper;
import com.wzdigit.wms.wms.dto.inventory.GoodsDto;
import com.wzdigit.wms.wms.rpc.inventory.GoodsApiImplService;
import com.wzdigit.wms.wms.task.IfGoodsSyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyf on 2021/12/29 19:29
 */
@Service
@Slf4j
public class GoodsSyncService {
    @Autowired
    private WipGoodsMapper wipGoodsMapper;

    @Autowired
    private IfGoodsSyncService ifGoodsSyncService;

    @Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 10000)
    BasicDataItemDetailApi basicDataItemDetailApi;

    @Value(value = "${default_owner.code}")
    private String DEFAULT_OWNER;

    @Value(value = "${default_owner.name}")
    private String DEFAULT_OWNER_NAME;



    public void syncData(){


        List<WipRcardCompletionDataDto> dtoDatas = wipGoodsMapper.searchSyncData();
        if (CollectionUtil.isEmpty(dtoDatas)){
            return;
        }
        log.info(">>>>>>>>>>Goods信息同步开始<<<<<<<<<<");
        log.info(dtoDatas.toString());
        //同步成功的DATA_ID集合
        DataItemDetailDTO dataItem = basicDataItemDetailApi.getDataItemDetailByPCodeAndName(DEFAULT_OWNER, DEFAULT_OWNER_NAME);
        for (WipRcardCompletionDataDto dto : dtoDatas) {
            try {
                GoodsDto goodsDto=new GoodsDto();
                goodsDto.setQaStatus(0);//0是待检

                goodsDto.setQty(new BigDecimal(1));
                goodsDto.setStatus(0);//未完成
                goodsDto.setIsHold(0);//非冻结
                goodsDto.setIsOriginalPackage(1);//原包装

                goodsDto.setOwner(dataItem == null || dataItem.getItemValue() == null ? 10001 : Integer.parseInt(dataItem.getItemValue()));
                goodsDto.setGoodsSn(dto.getRcardNo());
                goodsDto.setSkuCode(dto.getProductCode());//产品编码-》SKU_CODE
                goodsDto.setSoNo(dto.getSo());
                goodsDto.setSoItem(dto.getSoItem());
                goodsDto.setSoType(dto.getSoType());//销售订单类型
                goodsDto.setPoNo(dto.getPo());//PO
                Integer toSellFlag = dto.getToSellFlag();
                if (toSellFlag != null) {
                    goodsDto.setSellFlag(toSellFlag.toString());//内外销标识
                }
                goodsDto.setFactory(dto.getFactoryCode());//组织 5500
                goodsDto.setMfgPlant(dto.getFactoryCodeWorkShop());//分厂 一厂/二厂

                goodsDto.setPrdWo(dto.getOrderCode());//工单
                goodsDto.setPrdDate(dto.getCtime());
                goodsDto.setInspNo(dto.getQcorderCode());//检验单号
                goodsDto.setMfgLine(dto.getWorkLineCode());//产线
                ifGoodsSyncService.syncData(goodsDto,dto.getDataId());
            }catch (Exception e){
                log.info("---------["+dto.getRcardNo()+"]同步失败："+e.getMessage()+"---------");
            }
        }
    }

}
